package o;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.RemoteException;
import android.util.Log;
import com.huawei.hmf.orb.exception.ConnectRemoteException;
import com.huawei.hmf.orb.exception.ConnectRemoteException.e;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

public class ffp implements ffa, fgd, ServiceConnection, Callback {
    private List<fex> ʻ;
    private AtomicReference<d> ʼ;
    private HandlerThread ʽ;
    private Intent ˊ;
    private volatile ffw ˋ;
    private Context ˎ;
    private String ˏ;
    private String ॱ;
    private String ॱॱ;
    private Map<String, fhu> ᐝ;

    enum d {
        DISCONNECTED,
        CONNECTED,
        SUSPENDED
    }

    public ffj ˊ() {
        return ffd.ˋ;
    }

    public ffp(Context context, String str) {
        this(context, str, null);
    }

    public ffp(Context context, String str, String str2) {
        this(context, str, str2, null);
    }

    public ffp(Context context, String str, String str2, Intent intent) {
        this.ॱ = "com.huawei.hmf.remotemoduleservice";
        this.ʻ = new ArrayList();
        this.ᐝ = new HashMap();
        this.ʼ = new AtomicReference(d.DISCONNECTED);
        this.ˏ = str;
        this.ॱॱ = str + "/" + String.valueOf(hashCode());
        this.ˎ = context;
        if (str2 != null) {
            this.ॱ = str2;
        }
        this.ˊ = intent;
    }

    private Intent ॱॱ() {
        Intent intent = new Intent(this.ॱ);
        intent.setPackage(this.ˏ);
        fgx o_fgx = new fgx();
        o_fgx.packageName = ʼ();
        intent.putExtra(fgx.getDescriptor(), o_fgx.toBundle());
        if (this.ˊ != null) {
            intent.fillIn(this.ˊ, 0);
        }
        return intent;
    }

    public void ˏ(fex o_fex) throws ConnectRemoteException {
        ffo o_ffo = new ffo(o_fex);
        this.ʻ.add(o_ffo);
        if (ˏ()) {
            o_ffo.ˋ();
            return;
        }
        try {
            ॱˊ();
        } catch (ConnectRemoteException e) {
            o_ffo.ˊ(e);
            this.ʻ.remove(o_ffo);
            throw e;
        }
    }

    public void ॱ() {
        if (ˏ()) {
            ʻ();
            Iterator it = this.ʻ.iterator();
            while (it.hasNext()) {
                fex o_fex = (fex) it.next();
                o_fex.ˊ();
                if (o_fex instanceof ffo) {
                    it.remove();
                }
            }
        }
    }

    public boolean ˏ() {
        return this.ʼ.get() == d.CONNECTED;
    }

    public String ˎ() {
        return this.ॱॱ;
    }

    public String ᐝ() {
        return this.ॱॱ;
    }

    public String ʼ() {
        return this.ˎ.getPackageName();
    }

    public ffw ʽ() {
        return this.ˋ;
    }

    public Map<String, fhu> ˋ() {
        return this.ᐝ;
    }

    public void ˎ(fex o_fex) {
        this.ʻ.add(o_fex);
    }

    public void ˊ(fex o_fex) {
        this.ʻ.remove(o_fex);
    }

    private void ʻ() {
        try {
            if (ˏ() && this.ˎ != null) {
                fhd.ˎ(this).ˊ();
                this.ˎ.unbindService(this);
                this.ʼ.set(d.DISCONNECTED);
            }
        } catch (Exception e) {
            this.ʼ.set(d.DISCONNECTED);
        }
    }

    private void ॱˊ() throws ConnectRemoteException {
        if (!ˏॱ()) {
            throw new ConnectRemoteException(e.NotFoundService, "Not Found Service with package name " + this.ˏ);
        } else if (!this.ˎ.bindService(ॱॱ(), this, 1)) {
            this.ˎ.unbindService(this);
            throw new ConnectRemoteException(e.UnableBindService, "Unable bind to service with package name " + this.ˏ);
        }
    }

    private boolean ˏॱ() {
        try {
            if (this.ˎ.getPackageManager().getApplicationInfo(this.ˏ, 0) != null) {
                return true;
            }
            return false;
        } catch (NameNotFoundException e) {
            return false;
        }
    }

    private void ˊ(fhf.e eVar) {
        for (String str : eVar.getRemoteModules()) {
            ffz ˏ = ffz.ˏ(str);
            if (ˏ != null) {
                this.ᐝ.put(str, ˏ.ˊ());
            }
        }
    }

    private void ˋॱ() {
        fhf.ˎ(this).ˎ(new fgl<fgp<fhf.e>>(this) {
            final /* synthetic */ ffp ˎ;

            {
                this.ˎ = r1;
            }

            public /* synthetic */ void ˊ(Object obj) {
                ˋ((fgp) obj);
            }

            public void ˋ(fgp<fhf.e> o_fgp_o_fhf_e) {
                if (o_fgp_o_fhf_e == null || !o_fgp_o_fhf_e.ॱ().ˎ()) {
                    this.ˎ.ˏ(new ConnectRemoteException(e.RejectBindService, "Get module metadata error from remote repository, error code: " + (o_fgp_o_fhf_e != null ? Integer.valueOf(o_fgp_o_fhf_e.ॱ().ˏ()) : com.huawei.hwCloudJs.d.d.b)));
                    return;
                }
                this.ˎ.ˊ((fhf.e) o_fgp_o_fhf_e.ˏ());
                for (fex ˋ : this.ˎ.ʻ) {
                    ˋ.ˋ();
                }
            }
        });
    }

    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        if (this.ʽ != null) {
            this.ʽ.quit();
        }
        try {
            int i;
            String interfaceDescriptor = iBinder.getInterfaceDescriptor();
            if ("com.huawei.hmf.orb.aidl.IAIDLInvoke".equals(interfaceDescriptor)) {
                i = 0;
            } else if ("com.huawei.hmf.orb.aidl.IRemoteServiceBroker".equals(interfaceDescriptor)) {
                i = 1;
            } else {
                ˋ(207135000, null);
                return;
            }
            this.ʽ = new HandlerThread("RemoteServiceHandler");
            this.ʽ.start();
            Handler handler = new Handler(this.ʽ.getLooper(), this);
            handler.sendMessage(handler.obtainMessage(i, iBinder));
        } catch (RemoteException e) {
            ˋ(207135001, null);
        }
    }

    private void ˋ(int i, IBinder iBinder) {
        if (this.ʽ != null) {
            this.ʽ.quit();
            this.ʽ = null;
        }
        if (i == 0) {
            this.ˋ = ffw.e.ˋ(iBinder);
            this.ʼ.set(d.CONNECTED);
            ˋॱ();
            return;
        }
        this.ˎ.unbindService(this);
        this.ʼ.set(d.DISCONNECTED);
        final ConnectRemoteException connectRemoteException = new ConnectRemoteException(e.RejectBindService, "Connection rejected, error code: " + i);
        new Handler(Looper.getMainLooper()).post(new Runnable(this) {
            final /* synthetic */ ffp ˎ;

            public void run() {
                this.ˎ.ˏ(connectRemoteException);
            }
        });
    }

    private void ˏ(ConnectRemoteException connectRemoteException) {
        Iterator it = this.ʻ.iterator();
        while (it.hasNext()) {
            fex o_fex = (fex) it.next();
            o_fex.ˊ(connectRemoteException);
            if (o_fex instanceof ffo) {
                it.remove();
            }
        }
    }

    public void onServiceDisconnected(ComponentName componentName) {
        this.ʼ.set(d.SUSPENDED);
        Log.e("AIDLConnector", "service " + ʼ() + ": Connection Status set to SUSPENDED");
        for (fex ˊ : this.ʻ) {
            ˊ.ˊ();
        }
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public boolean handleMessage(android.os.Message r5) {
        /*
        r4 = this;
        r0 = r5.what;	 Catch:{ RemoteException -> 0x0010 }
        switch(r0) {
            case 0: goto L_0x0007;
            case 1: goto L_0x0019;
            default: goto L_0x0005;
        };	 Catch:{ RemoteException -> 0x0010 }
    L_0x0005:
        r0 = 1;
        return r0;
    L_0x0007:
        r0 = r5.obj;	 Catch:{ RemoteException -> 0x0010 }
        r0 = (android.os.IBinder) r0;	 Catch:{ RemoteException -> 0x0010 }
        r1 = 0;
        r4.ˋ(r1, r0);	 Catch:{ RemoteException -> 0x0010 }
        goto L_0x0005;
    L_0x0010:
        r0 = move-exception;
        r0 = 207135001; // 0xc58a119 float:1.6688513E-31 double:1.02338288E-315;
        r1 = 0;
        r4.ˋ(r0, r1);
        goto L_0x0005;
    L_0x0019:
        r0 = r5.obj;	 Catch:{ RemoteException -> 0x0010 }
        r0 = (android.os.IBinder) r0;	 Catch:{ RemoteException -> 0x0010 }
        r0 = o.ffs.e.ˏ(r0);	 Catch:{ RemoteException -> 0x0010 }
        r1 = new com.huawei.hmf.orb.aidl.impl.GetServiceRequest;	 Catch:{ RemoteException -> 0x0010 }
        r2 = 0;
        r3 = r4.ॱॱ();	 Catch:{ RemoteException -> 0x0010 }
        r1.<init>(r2, r3);	 Catch:{ RemoteException -> 0x0010 }
        r2 = new o.ffp$5;	 Catch:{ RemoteException -> 0x0010 }
        r2.<init>(r4);	 Catch:{ RemoteException -> 0x0010 }
        r0.ॱ(r1, r2);	 Catch:{ RemoteException -> 0x0010 }
        goto L_0x0005;
        */
        throw new UnsupportedOperationException("Method not decompiled: o.ffp.handleMessage(android.os.Message):boolean");
    }
}
